#include "stdafx.h"

using namespace System;
using namespace System::Collections;
using namespace System::Data;
using namespace System::Collections::Generic;
using namespace Librarian::Data;
using namespace Librarian::Entities;
using namespace NUnit::Framework;


namespace Librarian
{
	[TestFixture]
	public ref class UnitTest
	{
	private :
		static String^ ConnectionString = "Data Source=localhost;Initial Catalog=librarian;Integrated Security=True";
		IDataProvider^ _provider;
	public:
		property IDataProvider^ Provider
		{
			IDataProvider^ get()
			{
				if (_provider == nullptr)
				{
					_provider = gcnew SqlDataProvider();
					_provider->ConnectionString  = ConnectionString;
				}
				return _provider;
			}
		}
		[Test]
		void can_list_admin()
		{
			array<AdminEntity^>^ admins = this->Provider->ListAdmin();
		}
		[Test]
		void validate_admin()
		{
			Assert::AreEqual(0, this->Provider->ValidateAdmin("admin", ""));
		}
		[Test]
		void can_fetch_admin()
		{
			array<AdminEntity^>^ admins = this->Provider->ListAdmin();
			for (int i=0; i<admins->Length; i++)
			{
				AdminEntity^ result = this->Provider->FetchAdmin("lugi");
				Assert::AreEqual(result->AdminId, "lugi");
			}
		}
		[Test]
		void can_fetch_book()
		{
			array<BookEntity^>^ books = this->Provider->ListBook();
			int flag = 0;
			for (int i=0; i<books->Length; i++)
			{
				if (books[i]->BookNo == "013")
				{
					flag = 1;
					break;
				}
			}
			Assert::AreEqual(flag, 1);
		}
		[Test]
		void can_borrow_book()
		{
			Assert::AreEqual(0, this->Provider->BorrowBook("011", "12345", "lugi"));
			Assert::AreEqual(3, this->Provider->BorrowBook("021", "12345", "lugi"));
			Assert::AreEqual(2, this->Provider->BorrowBook("008", "12333", "lugi"));
			Assert::AreEqual(1, this->Provider->BorrowBook ("001","12350","michael"));
		}
		[Test]
		void can_return_book()
		{
			this->Provider->BorrowBook("011", "12345", "lugi");
			Assert::AreEqual(2, this->Provider->ReturnBook("011","12344", "lugi"));
			Assert::AreEqual(0, this->Provider->ReturnBook("011","12345", "lugi"));
		}

		[Test]
		void can_delete_book()
		{
			this->Provider->BorrowBook("012", "12345", "lugi");
			this->Provider->BorrowBook("017", "12345", "lugi");
			this->Provider->BorrowBook("017", "12345", "lugi");
			Assert::AreEqual(1, this->Provider->DeleteBook("034"));
			Assert::AreEqual(2, this->Provider->DeleteBook("017"));
			Assert::AreEqual(0, this->Provider->DeleteBook("001"));

		}
		[Test]
		void can_list_book()
		{
			array<BookEntity^>^ books = this->Provider->ListBook();
		}
		[Test]
		void can_add_card()
		{
			Assert::AreEqual(0, this->Provider->AddCard("12354", "huhu", "CS", 'T'));
			Assert::AreEqual(1, this->Provider->AddCard("12345", "huhu", "CS", 'T'));
		}
		[Test]
		void can_delete_card()
		{
			Assert::AreEqual(1, this->Provider->DeleteCard("12211"));
			this->Provider->BorrowBook("011", "12346", "lugi");
			Assert::AreEqual(2, this->Provider->DeleteCard("12346"));
			this->Provider->AddCard("12399", "xx", "ss", 'G');
			Assert::AreEqual(1, this->Provider->DeleteCard("12399"));
		}

		[Test]
		void can_query_book()
		{
			this->Provider->SearchBooks("music", "","",0,0, "", 0, 0);
		}
	};

}

